<template>
	<view class="content">
		<view class="head-img">
			<swiper class="swiper-box" indicator-dots indicator-color="#cccccc" indicator-active-color="#f18b49" acceleration>
				<swiper-item class="swiper-item" v-for="(item,index) in proImg" :key="index">
					<image :src="item" mode="widthFix" @click="prevImage(index)"></image>
				</swiper-item>
			</swiper>
		</view>
		<view class="pro-details">
			<view class="pro-detai-box">
				<text class="pro-id">商品编号：{{ goods_no }}</text>
				<view class="price-proid">
					<view class="price">
						<text class="price-icon">￥</text>
						<u-count-to :startVal="0" :endVal="product.sell_price" :decimals="2" fontSize="38rpx" color="#f18b49" style="margin-right: 15rpx;"></u-count-to>
					</view>
					<view class="pro-detai-bot">
						<text>销量：{{ sale }}</text>
						<text style="margin-left: 30rpx;">库存：{{ product.store_nums }}</text>
					</view>
				</view>
				<view class="pro-detai-bot">
					<text>税率：13%</text>
					<text>是否含运费：是</text>
					<text>安装：是</text>
					<text>起订量：1</text>
				</view>
			
				<view class="pro-tit">{{ name }}</view>
				<view v-if="JSON.stringify(specObj) == '[]'" class="pro-select">
					<text style="margin-right: 100rpx;">规格参数</text>
					<view class="sel-class-box">该商品无规格选择</view>
				</view>
				<!-- 规格 -->
				<view v-else class="pro-select" v-for="(item,index) in specObj" :key="index">
					<text style="margin-right: 50rpx;">{{ item.name }}</text>
					<view class="sel-class-box">
						<view class="sel-class">
							<view class="message-class" :style="{border:myStyle(index,message)?'1rpx solid #f18b49':''}"
								@click="addBorder(index,item,message)" v-for="(message,i) in item.value" :key="i">
								{{message.name}}</view>
						</view>
					</view>
				</view>
				<view class="pro-number">
					<text>购买数量</text>
					<u-number-box v-model="buyNum" @change="valChange" @overlimit="overLimit" integer :inputWidth="50"
						:buttonSize="35" color="#f18b49" :min="1" :max="9999"></u-number-box>
				</view>
				<view class="pro-detai-bot">
					<text class="">退换货：七天无理由（食品及定制除外）</text>
				</view>
			</view>
		</view>
		
		<u-divider text="商品详情"></u-divider>
		<view class="cont-img">
			<image :src="item" mode="widthFix" lazy-load v-for="(item,index) in contImg"></image>
		</view>
		<view class="bot-nav">
			<view class="nav-left">
				<view class="left-box-2">
					<uni-icons type="headphones" color="#f18b49" size="30"></uni-icons>
					<view class="left-txt-2">客服</view>
				</view>
				<view class="left-box-1" @click="collect(isStar)">
					<uni-icons :type="isStar ? 'star-filled':'star' " color="#f18b49" size="30"></uni-icons>
					<view class="left-txt-1">收藏</view>
				</view>
			</view>
			<view class="nav-right">
				<view class="nav-right-leftbtn" @click="addCart(proId)">加入购物车</view>
				<view class="nav-right-rightbtn" @click="onceBuy(product.id)">立即购买</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				name: '',
				sale: '',
				token: '',
				proId: '',
				userId: '',
				buyNum: 1,
				current: 0,
				proImg: [],
				contImg: [],
				specObj: [],
				product: {},
				favorite: '',
				goodType: '',
				goods_no: '',
				isStar: false,
				jiesuanList: [],
				specChooseList: [],
			}
		},
		onLoad(e) {
			this.proId = e.proId
			this.userId = e.userId
			this.getDetail(this.proId)
			setTimeout(() => {
				if( JSON.stringify(this.specObj) == '[]'){
					this.goodType = 'goods'
				} else {
					this.goodType = 'product'
				}
			},500)
		},
		onShow() {
			this.token = uni.getStorageSync('token')
		},
		watch:{
			favorite:{
				handler:function(newvalue){
					newvalue == 0 ? this.isStar = false : this.isStar = true
				},
				immediate:true,
			}
		},
		computed: {
			myStyle() {
				return function(index, message) {
					if (this.specChooseList[index] != undefined) {
						if (this.specChooseList[index].value == message.name) {
							return true
						} else{
							return false
						}
					}
				}
			}
		},
		methods: {
			// 获取商品详情
			getDetail(id) {
				let baseUrl = 'https://gjc.hy-dzsc.cn/'
				this.$getHttp(`/gjcApi/getGoodsProducts?id=${id}&user_id=${this.userId}`, {}, res => {
					this.product = res
					this.goods_no = res.goods_no
					this.sale = res.sale
					this.name = res.name
					let imgArr = []
					this.proImg = []
					imgArr = Object.values(res.content)
					imgArr.forEach(v => {
						this.contImg.push(baseUrl + v)
					})
					this.favorite = res.favorite_id
					for (let item of res.photo) {
						this.proImg.push(baseUrl + item.img)
					}
					this.specObj = []
					let spec = res.spec_array
					for (let i in spec) {
						this.specObj.push(spec[i])
						let valueArr = this.specObj
					}
				})
			},
			// 收藏商品
			collect(e) {
				if (this.token) {
					if (e != true) {
						this.$http(`/gjcApi/favoriteAdd?goods_id=${this.proId}&user_id=${this.userId}&id=0`, {}, res => {
							if(res.code == 200){
								uni.showToast({
									title: '收藏成功',
									duration: 1000,
									icon: 'none'
								})
								this.getDetail(this.proId)
							} 
						})
					} else {
						this.$http(`/gjcApi/favoriteAdd?goods_id=${this.proId}&user_id=${this.userId}&id=${this.favorite}`, {}, res => {
							if(res.code == 200){
								this.favorite = 0
								uni.showToast({
									title: '取消收藏',
									duration: 1000,
									icon: 'none'
								})
							} 
						})
					}
				} else {
					uni.showModal({
						title: '登录验证',
						cancelText: '先逛逛',
						confirmText: '去登录',
						confirmColor: '#f18b49',
						content: '账号未登录，无法收藏该商品！',
						success: (res) => {
							if (res.confirm) {
								uni.navigateTo({
									url: '/pages/login/code-login'
								})
							}
						}
					})
				}
			},
			// 加入购物车
			addCart(id) {
				if(this.specChooseList.length != this.specObj.length){
					uni.showToast({
						icon:"none",
						title:"请先选择商品规格参数",
						duration:1500
					})
					return
				}else{
					for(let i =0;i<this.specObj.length;i++){
						if(this.specChooseList[i]==undefined){
							uni.showToast({
								title: '请先选择商品规格参数',
								duration: 1500,
								icon: 'none'
							})
							return;
						}
					}		
				}
				if (this.token) {
					if(this.product.id){
						this.joinCartPack(this.product.id)
					} else {
						this.joinCartPack(id)
					}
				} else {
					uni.showModal({
						title: '登录验证',
						cancelText: '先逛逛',
						confirmText: '去登录',
						confirmColor: '#f18b49',
						content: '账号未登录，无法添加到购物车！',
						success: (res) => {
							if (res.confirm) {
								uni.navigateTo({
									url: '/pages/login/code-login'
								})
							}
						}
					})
				}
			},
			// 加入购物车封装
			joinCartPack(id){
				this.$getHttp('/gjcApi/joinCart?goods_id=' + id + '&type=' +this.goodType + '&goods_num=' + this.buyNum, {
					user_id: this.userId
				}, res => {
					if (res.isError == 406) {
						uni.showToast({
							title: '请先选择商品规格参数',
							duration: 1500,
							icon: 'none'
						})
					} else if (res.isError == 407) {
						uni.showToast({
							title: '该商品库存不足，再等等',
							duration: 1500,
							icon: 'none'
						})
					} else if (res.isError == 408) {
						uni.showToast({
							title: '加入购物车失败，已超出购物车容量',
							duration: 1500,
							icon: 'none'
						})
					} else {
						uni.showToast({
							title: '已加入购物车',
							duration: 1000,
							icon: 'none'
						})
					}
				})
			},
			// 获取商品价格
			valChange(e) {
				this.buyNum = e.value
			},
			// 商品数量最大或最小时触发  minus-最小  plus-最大
			overLimit(e) {
				if (e == 'minus') {
					uni.showToast({
						title: '购买数量不能少于1',
						duration: 1000,
						icon: 'none'
					})
				} else if (e == 'plus') {
					uni.showToast({
						title: '购买数量不能超过9999',
						duration: 1000,
						icon: 'none'
					})
				}
			},
			// 立即购买
			onceBuy(id) {
				this.jiesuanList = []
				if(this.specChooseList.length != this.specObj.length){
					uni.showToast({
						icon:"none",
						title:"请先选择商品规格参数",
						duration:1500
					})
					return
				}else{
					for(let i =0;i<this.specObj.length;i++){
						if(this.specChooseList[i]==undefined){
							uni.showToast({
								title: '请先选择商品规格参数',
								duration: 1500,
								icon: 'none'
							})
							return;
						}
					}		
				}
				if(this.specObj.length == 0){
					this.goodType = 'goods'
				} else {
					this.goodType = 'product'
				}
				let json = {}
				json.id = id
				json.count = this.buyNum
				json.type = this.goodType
				this.jiesuanList.push(json)
				const data = {
					list: this.jiesuanList,
					sum: this.product.sell_price,
					user_id: this.userId
				}
				uni.navigateTo({
					url: `/pages/cart/confirm-order?data=${JSON.stringify(data)}&userId=${data.user_id}`
				})
			},
			// 选择规格选择
			addBorder(index, item, value) {
				const obj = {
					id: item.id,
					type: item.type,
					value: value.name,
					name: item.name,
					tip: value.tip
				}
				this.$set(this.specChooseList,index,obj)
				if(this.specChooseList.length != this.specObj.length){
					return
					uni.showToast({
						icon:"none",
						title:"请先选择商品规格参数",
						duration:1500
					})
				}else{
					for(let i =0;i<this.specObj.length;i++){
						if(this.specChooseList[i]==undefined){
							uni.showToast({
								title: '请先选择商品规格参数',
								duration: 1500,
								icon: 'none'
							})
							return;
						}
					}		
				}
				this.$http(`/gjcApi/getProducts?goods_id=${this.proId}&user_id=${this.userId}`,{data: JSON.stringify(this.specChooseList)}, res=>{
					if(res.flag == 'success'){
						this.product = res.data
					}
				})
			},
			// 预览图片
			prevImage(index) {
				uni.previewImage({
					current: index,
					urls: this.proImg,
					indicator: 'number'
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		.head-img {
			.swiper-box {
				height: 750rpx;
			}
			.swiper-item {
				image {
					width: 100%;
				}
			}
		}
		.pro-details {
			padding: 20rpx 15rpx 0 15rpx;

			.pro-detai-box {
				border-radius: 20rpx;
				background: $uni-model-color;
				padding: 25rpx 25rpx 30rpx 25rpx;
				.pro-id {
					display: flex;
					font-size: 26rpx;
					justify-content: flex-end;
					color: $uni-text-color-disable;
				}
				.pro-detai-bot {
					display: flex;
					font-size: 26rpx;
					align-items: center;
					color: $uni-text-color-grey;
					justify-content: space-between;
				
					
				}
				
				.price-proid {
					display: flex;
					align-items: baseline;
					margin: 10rpx 0 15rpx 0;
					justify-content: space-between;

					.price {
						.price-icon {
							font-size: 26rpx;
							color: $uni-text-color;
						}

						.pro-price {
							font-size: 40rpx;
							color: $uni-text-color;
						}
					}
				}

				.pro-tit {
					font-size: 28rpx;
					font-weight: bold;
					letter-spacing: 1rpx;
				}

				.pro-select {                                    
					display: flex;
					font-size: 28rpx;
					margin: 35rpx 0 30rpx 0;
					color: $uni-text-color-grey;

					text {
						flex-shrink: 0;
					}

					.sel-class-box {
						display: flex;
						flex-wrap: wrap;

						.sel-class {
							display: flex;
							// padding: 14rpx 35rpx;
							margin: 0 20rpx 15rpx 0;

							// background: $uni-border-color;
							.message-class {
								margin: 0 20rpx;
								background: #efefef;
								padding: 15rpx 40rpx;
							}

							.sel-class-active {
								padding: 12rpx 35rpx;
								color: $uni-text-color;
								margin: 0 20rpx 15rpx 0;
								border: 2rpx solid $uni-text-color;
							}
						}
					}
				}

				.pro-number {
					display: flex;
					font-size: 28rpx;
					align-items: center;
					color: $uni-text-color-grey;
					justify-content: space-between;
				}
			}
		}

		.cont-img {
			padding-bottom: 95rpx;

			image {
				width: 100%;
			}
		}

		.bot-nav {
			display: flex;
			width: 750rpx;
			position: fixed;
			padding: 10rpx 0rpx;
			align-items: center;
			background: $uni-model-color;
			bottom: var(--window-bottom, 0);
			justify-content: space-between;
			border-top: 2rpx solid $uni-border-color;

			.nav-left {
				display: flex;
				text-align: center;
				padding-left: 30rpx;
				color: $uni-text-color-grey;

				.left-box-1 {
					.left-txt-1 {
						font-size: 22rpx;
					}
				}

				.left-box-2 {
					margin-right: 35rpx;

					.left-txt-2 {
						font-size: 22rpx;
					}
				}
			}

			.nav-right {
				display: flex;
				font-size: 26rpx;
				margin-right: 20rpx;
				align-items: center;
				color: $uni-model-color;

				.nav-right-leftbtn {
					background: red;
					padding: 24rpx 70rpx;
					border-radius: 50rpx 0 0 50rpx;
				}

				.nav-right-rightbtn {
					background: #ffa200;
					padding: 24rpx 83rpx;
					border-radius: 0 50rpx 50rpx 0;
				}
			}
		}
	}
</style>